package com.uniflow.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.uniflow.common.Result;
import com.uniflow.entity.ThirdPartyIntegration;
import com.uniflow.service.ThirdPartyIntegrationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 第三方集成管理控制器
 */
@Api(tags = "第三方集成管理")
@RestController
@RequestMapping("/api/v1/third-party-integration")
public class ThirdPartyIntegrationController {
    
    @Autowired
    private ThirdPartyIntegrationService thirdPartyIntegrationService;
    
    /**
     * 分页查询第三方集成
     */
    @ApiOperation("分页查询第三方集成")
    @GetMapping("/page")
    public Result<IPage<ThirdPartyIntegration>> getPageList(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @ApiParam("页大小") @RequestParam(defaultValue = "10") Integer pageSize,
            @ApiParam("集成名称") @RequestParam(required = false) String name,
            @ApiParam("集成类型") @RequestParam(required = false) String integrationType,
            @ApiParam("系统类型") @RequestParam(required = false) String systemType,
            @ApiParam("状态") @RequestParam(required = false) String status,
            @ApiParam("连接状态") @RequestParam(required = false) String connectionStatus,
            @ApiParam("分类") @RequestParam(required = false) String category,
            @ApiParam("是否启用") @RequestParam(required = false) Boolean enabled,
            @ApiParam("创建人") @RequestParam(required = false) String createdBy,
            @ApiParam("开始时间") @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @ApiParam("结束时间") @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime,
            @ApiParam("关键词") @RequestParam(required = false) String keyword,
            HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        IPage<ThirdPartyIntegration> result = thirdPartyIntegrationService.getPageList(pageNum, pageSize, name,
                integrationType, systemType, status, connectionStatus, category, enabled, createdBy,
                startTime, endTime, keyword, tenantId);
        return Result.success(result);
    }
    
    /**
     * 根据ID查询第三方集成
     */
    @ApiOperation("根据ID查询第三方集成")
    @GetMapping("/{id}")
    public Result<ThirdPartyIntegration> getById(@ApiParam("第三方集成ID") @PathVariable String id,
                                                 HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        ThirdPartyIntegration integration = thirdPartyIntegrationService.getById(id, tenantId);
        return Result.success(integration);
    }
    
    /**
     * 根据名称查询第三方集成
     */
    @ApiOperation("根据名称查询第三方集成")
    @GetMapping("/name/{name}")
    public Result<List<ThirdPartyIntegration>> getByName(@ApiParam("集成名称") @PathVariable String name,
                                                         HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<ThirdPartyIntegration> list = thirdPartyIntegrationService.getByName(name, tenantId);
        return Result.success(list);
    }
    
    /**
     * 根据外部ID查询第三方集成
     */
    @ApiOperation("根据外部ID查询第三方集成")
    @GetMapping("/external-id/{externalId}")
    public Result<ThirdPartyIntegration> getByExternalId(@ApiParam("外部ID") @PathVariable String externalId,
                                                         HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        ThirdPartyIntegration integration = thirdPartyIntegrationService.getByExternalId(externalId, tenantId);
        return Result.success(integration);
    }
    
    /**
     * 根据业务键查询第三方集成
     */
    @ApiOperation("根据业务键查询第三方集成")
    @GetMapping("/business-key/{businessKey}")
    public Result<ThirdPartyIntegration> getByBusinessKey(@ApiParam("业务键") @PathVariable String businessKey,
                                                          HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        ThirdPartyIntegration integration = thirdPartyIntegrationService.getByBusinessKey(businessKey, tenantId);
        return Result.success(integration);
    }
    
    /**
     * 根据集成类型查询第三方集成
     */
    @ApiOperation("根据集成类型查询第三方集成")
    @GetMapping("/integration-type/{integrationType}")
    public Result<List<ThirdPartyIntegration>> getByIntegrationType(@ApiParam("集成类型") @PathVariable String integrationType,
                                                                    HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<ThirdPartyIntegration> list = thirdPartyIntegrationService.getByIntegrationType(integrationType, tenantId);
        return Result.success(list);
    }
    
    /**
     * 根据系统类型查询第三方集成
     */
    @ApiOperation("根据系统类型查询第三方集成")
    @GetMapping("/system-type/{systemType}")
    public Result<List<ThirdPartyIntegration>> getBySystemType(@ApiParam("系统类型") @PathVariable String systemType,
                                                               HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<ThirdPartyIntegration> list = thirdPartyIntegrationService.getBySystemType(systemType, tenantId);
        return Result.success(list);
    }
    
    /**
     * 根据状态查询第三方集成
     */
    @ApiOperation("根据状态查询第三方集成")
    @GetMapping("/status/{status}")
    public Result<List<ThirdPartyIntegration>> getByStatus(@ApiParam("状态") @PathVariable String status,
                                                           HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<ThirdPartyIntegration> list = thirdPartyIntegrationService.getByStatus(status, tenantId);
        return Result.success(list);
    }
    
    /**
     * 根据连接状态查询第三方集成
     */
    @ApiOperation("根据连接状态查询第三方集成")
    @GetMapping("/connection-status/{connectionStatus}")
    public Result<List<ThirdPartyIntegration>> getByConnectionStatus(@ApiParam("连接状态") @PathVariable String connectionStatus,
                                                                     HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<ThirdPartyIntegration> list = thirdPartyIntegrationService.getByConnectionStatus(connectionStatus, tenantId);
        return Result.success(list);
    }
    
    /**
     * 根据分类查询第三方集成
     */
    @ApiOperation("根据分类查询第三方集成")
    @GetMapping("/category/{category}")
    public Result<List<ThirdPartyIntegration>> getByCategory(@ApiParam("分类") @PathVariable String category,
                                                             HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<ThirdPartyIntegration> list = thirdPartyIntegrationService.getByCategory(category, tenantId);
        return Result.success(list);
    }
    
    /**
     * 根据创建人查询第三方集成
     */
    @ApiOperation("根据创建人查询第三方集成")
    @GetMapping("/created-by/{createdBy}")
    public Result<List<ThirdPartyIntegration>> getByCreatedBy(@ApiParam("创建人ID") @PathVariable String createdBy,
                                                              HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<ThirdPartyIntegration> list = thirdPartyIntegrationService.getByCreatedBy(createdBy, tenantId);
        return Result.success(list);
    }
    
    /**
     * 查询启用的第三方集成
     */
    @ApiOperation("查询启用的第三方集成")
    @GetMapping("/enabled")
    public Result<List<ThirdPartyIntegration>> getEnabledIntegrations(HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<ThirdPartyIntegration> list = thirdPartyIntegrationService.getEnabledIntegrations(tenantId);
        return Result.success(list);
    }
    
    /**
     * 查询可连接的第三方集成
     */
    @ApiOperation("查询可连接的第三方集成")
    @GetMapping("/connectable")
    public Result<List<ThirdPartyIntegration>> getConnectableIntegrations(HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<ThirdPartyIntegration> list = thirdPartyIntegrationService.getConnectableIntegrations(tenantId);
        return Result.success(list);
    }
    
    /**
     * 查询自动同步的第三方集成
     */
    @ApiOperation("查询自动同步的第三方集成")
    @GetMapping("/auto-sync")
    public Result<List<ThirdPartyIntegration>> getAutoSyncIntegrations(HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<ThirdPartyIntegration> list = thirdPartyIntegrationService.getAutoSyncIntegrations(tenantId);
        return Result.success(list);
    }
    
    /**
     * 查询实时同步的第三方集成
     */
    @ApiOperation("查询实时同步的第三方集成")
    @GetMapping("/realtime-sync")
    public Result<List<ThirdPartyIntegration>> getRealtimeSyncIntegrations(HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<ThirdPartyIntegration> list = thirdPartyIntegrationService.getRealtimeSyncIntegrations(tenantId);
        return Result.success(list);
    }
    
    /**
     * 查询正在同步的第三方集成
     */
    @ApiOperation("查询正在同步的第三方集成")
    @GetMapping("/syncing")
    public Result<List<ThirdPartyIntegration>> getSyncingIntegrations(HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<ThirdPartyIntegration> list = thirdPartyIntegrationService.getSyncingIntegrations(tenantId);
        return Result.success(list);
    }
    
    /**
     * 查询有依赖关系的第三方集成
     */
    @ApiOperation("查询有依赖关系的第三方集成")
    @GetMapping("/dependent/{integrationId}")
    public Result<List<ThirdPartyIntegration>> getDependentIntegrations(@ApiParam("第三方集成ID") @PathVariable String integrationId,
                                                                        HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<ThirdPartyIntegration> list = thirdPartyIntegrationService.getDependentIntegrations(integrationId, tenantId);
        return Result.success(list);
    }
    
    /**
     * 创建第三方集成
     */
    @ApiOperation("创建第三方集成")
    @PostMapping
    public Result<ThirdPartyIntegration> createIntegration(@RequestBody ThirdPartyIntegration integration,
                                                           Authentication authentication,
                                                           HttpServletRequest request) {
        String currentUserId = authentication.getName();
        String tenantId = (String) request.getAttribute("tenantId");
        integration.setTenantId(tenantId);
        integration.setCreatedBy(currentUserId);
        integration.setCreatedByName(authentication.getName());
        ThirdPartyIntegration result = thirdPartyIntegrationService.createIntegration(integration);
        return Result.success(result);
    }
    
    /**
     * 更新第三方集成
     */
    @ApiOperation("更新第三方集成")
    @PutMapping("/{id}")
    public Result<ThirdPartyIntegration> updateIntegration(@ApiParam("第三方集成ID") @PathVariable String id,
                                                           @RequestBody ThirdPartyIntegration integration,
                                                           Authentication authentication,
                                                           HttpServletRequest request) {
        String currentUserId = authentication.getName();
        String tenantId = (String) request.getAttribute("tenantId");
        integration.setId(id);
        integration.setTenantId(tenantId);
        integration.setUpdatedBy(currentUserId);
        integration.setUpdatedByName(authentication.getName());
        ThirdPartyIntegration result = thirdPartyIntegrationService.updateIntegration(integration);
        return Result.success(result);
    }
    
    /**
     * 删除第三方集成
     */
    @ApiOperation("删除第三方集成")
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteIntegration(@ApiParam("第三方集成ID") @PathVariable String id) {
        boolean result = thirdPartyIntegrationService.removeById(id);
        return Result.success(result);
    }
    
    /**
     * 批量删除第三方集成
     */
    @ApiOperation("批量删除第三方集成")
    @DeleteMapping("/batch")
    public Result<Boolean> batchDeleteIntegrations(@RequestBody BatchDeleteRequest request) {
        boolean result = thirdPartyIntegrationService.batchDeleteIntegrations(request.getIds());
        return Result.success(result);
    }
    
    /**
     * 启用第三方集成
     */
    @ApiOperation("启用第三方集成")
    @PostMapping("/{id}/enable")
    public Result<Boolean> enableIntegration(@ApiParam("第三方集成ID") @PathVariable String id,
                                             Authentication authentication) {
        String currentUserId = authentication.getName();
        boolean result = thirdPartyIntegrationService.enableIntegration(id, currentUserId, authentication.getName());
        return Result.success(result);
    }
    
    /**
     * 禁用第三方集成
     */
    @ApiOperation("禁用第三方集成")
    @PostMapping("/{id}/disable")
    public Result<Boolean> disableIntegration(@ApiParam("第三方集成ID") @PathVariable String id,
                                              Authentication authentication) {
        String currentUserId = authentication.getName();
        boolean result = thirdPartyIntegrationService.disableIntegration(id, currentUserId, authentication.getName());
        return Result.success(result);
    }
    
    /**
     * 连接第三方集成
     */
    @ApiOperation("连接第三方集成")
    @PostMapping("/{id}/connect")
    public Result<Boolean> connectIntegration(@ApiParam("第三方集成ID") @PathVariable String id) {
        boolean result = thirdPartyIntegrationService.connectIntegration(id);
        return Result.success(result);
    }
    
    /**
     * 断开第三方集成
     */
    @ApiOperation("断开第三方集成")
    @PostMapping("/{id}/disconnect")
    public Result<Boolean> disconnectIntegration(@ApiParam("第三方集成ID") @PathVariable String id) {
        boolean result = thirdPartyIntegrationService.disconnectIntegration(id);
        return Result.success(result);
    }
    
    /**
     * 开始同步
     */
    @ApiOperation("开始同步")
    @PostMapping("/{id}/start-sync")
    public Result<Boolean> startSync(@ApiParam("第三方集成ID") @PathVariable String id,
                                     Authentication authentication) {
        String currentUserId = authentication.getName();
        boolean result = thirdPartyIntegrationService.startSync(id, currentUserId, authentication.getName());
        return Result.success(result);
    }
    
    /**
     * 停止同步
     */
    @ApiOperation("停止同步")
    @PostMapping("/{id}/stop-sync")
    public Result<Boolean> stopSync(@ApiParam("第三方集成ID") @PathVariable String id,
                                    Authentication authentication) {
        String currentUserId = authentication.getName();
        boolean result = thirdPartyIntegrationService.stopSync(id, currentUserId, authentication.getName());
        return Result.success(result);
    }
    
    /**
     * 重置第三方集成
     */
    @ApiOperation("重置第三方集成")
    @PostMapping("/{id}/reset")
    public Result<Boolean> resetIntegration(@ApiParam("第三方集成ID") @PathVariable String id,
                                            Authentication authentication) {
        String currentUserId = authentication.getName();
        boolean result = thirdPartyIntegrationService.resetIntegration(id, currentUserId, authentication.getName());
        return Result.success(result);
    }
    
    /**
     * 复制第三方集成
     */
    @ApiOperation("复制第三方集成")
    @PostMapping("/{id}/copy")
    public Result<ThirdPartyIntegration> copyIntegration(@ApiParam("第三方集成ID") @PathVariable String id,
                                                         @RequestBody CopyIntegrationRequest request,
                                                         Authentication authentication,
                                                         HttpServletRequest httpRequest) {
        String currentUserId = authentication.getName();
        String tenantId = (String) httpRequest.getAttribute("tenantId");
        ThirdPartyIntegration result = thirdPartyIntegrationService.copyIntegration(id, request.getNewName(),
                currentUserId, authentication.getName(), tenantId);
        return Result.success(result);
    }
    
    /**
     * 批量更新状态
     */
    @ApiOperation("批量更新状态")
    @PutMapping("/batch/status")
    public Result<Boolean> batchUpdateStatus(@RequestBody BatchUpdateStatusRequest request,
                                             Authentication authentication) {
        String currentUserId = authentication.getName();
        boolean result = thirdPartyIntegrationService.batchUpdateStatus(request.getIds(), request.getStatus(),
                currentUserId, authentication.getName());
        return Result.success(result);
    }
    
    /**
     * 批量更新优先级
     */
    @ApiOperation("批量更新优先级")
    @PutMapping("/batch/priority")
    public Result<Boolean> batchUpdatePriority(@RequestBody BatchUpdatePriorityRequest request,
                                               Authentication authentication) {
        String currentUserId = authentication.getName();
        boolean result = thirdPartyIntegrationService.batchUpdatePriority(request.getIds(), request.getPriority(),
                currentUserId, authentication.getName());
        return Result.success(result);
    }
    
    /**
     * 批量更新分类
     */
    @ApiOperation("批量更新分类")
    @PutMapping("/batch/category")
    public Result<Boolean> batchUpdateCategory(@RequestBody BatchUpdateCategoryRequest request,
                                               Authentication authentication) {
        String currentUserId = authentication.getName();
        boolean result = thirdPartyIntegrationService.batchUpdateCategory(request.getIds(), request.getCategory(),
                currentUserId, authentication.getName());
        return Result.success(result);
    }
    
    /**
     * 批量更新排序
     */
    @ApiOperation("批量更新排序")
    @PutMapping("/batch/sort-order")
    public Result<Boolean> batchUpdateSortOrder(@RequestBody BatchUpdateSortOrderRequest request,
                                                Authentication authentication) {
        String currentUserId = authentication.getName();
        boolean result = thirdPartyIntegrationService.batchUpdateSortOrder(request.getIds(), request.getSortOrder(),
                currentUserId, authentication.getName());
        return Result.success(result);
    }
    
    /**
     * 检查名称是否可用
     */
    @ApiOperation("检查名称是否可用")
    @GetMapping("/check-name")
    public Result<Boolean> checkNameAvailable(@ApiParam("集成名称") @RequestParam String name,
                                              @ApiParam("排除的ID") @RequestParam(required = false) String excludeId,
                                              HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        boolean available = thirdPartyIntegrationService.isNameAvailable(name, excludeId, tenantId);
        return Result.success(available);
    }
    
    /**
     * 检查业务键是否可用
     */
    @ApiOperation("检查业务键是否可用")
    @GetMapping("/check-business-key")
    public Result<Boolean> checkBusinessKeyAvailable(@ApiParam("业务键") @RequestParam String businessKey,
                                                     @ApiParam("排除的ID") @RequestParam(required = false) String excludeId,
                                                     HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        boolean available = thirdPartyIntegrationService.isBusinessKeyAvailable(businessKey, excludeId, tenantId);
        return Result.success(available);
    }
    
    /**
     * 获取第三方集成统计信息
     */
    @ApiOperation("获取第三方集成统计信息")
    @GetMapping("/statistics")
    public Result<Map<String, Object>> getStatistics(HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        Map<String, Object> statistics = thirdPartyIntegrationService.getStatistics(tenantId);
        return Result.success(statistics);
    }
    
    /**
     * 按状态统计第三方集成数量
     */
    @ApiOperation("按状态统计第三方集成数量")
    @GetMapping("/statistics/status")
    public Result<List<Map<String, Object>>> getStatsByStatus(HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<Map<String, Object>> stats = thirdPartyIntegrationService.getStatsByStatus(tenantId);
        return Result.success(stats);
    }
    
    /**
     * 按集成类型统计第三方集成数量
     */
    @ApiOperation("按集成类型统计第三方集成数量")
    @GetMapping("/statistics/integration-type")
    public Result<List<Map<String, Object>>> getStatsByIntegrationType(HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<Map<String, Object>> stats = thirdPartyIntegrationService.getStatsByIntegrationType(tenantId);
        return Result.success(stats);
    }
    
    /**
     * 按系统类型统计第三方集成数量
     */
    @ApiOperation("按系统类型统计第三方集成数量")
    @GetMapping("/statistics/system-type")
    public Result<List<Map<String, Object>>> getStatsBySystemType(HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<Map<String, Object>> stats = thirdPartyIntegrationService.getStatsBySystemType(tenantId);
        return Result.success(stats);
    }
    
    /**
     * 按连接状态统计第三方集成数量
     */
    @ApiOperation("按连接状态统计第三方集成数量")
    @GetMapping("/statistics/connection-status")
    public Result<List<Map<String, Object>>> getStatsByConnectionStatus(HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<Map<String, Object>> stats = thirdPartyIntegrationService.getStatsByConnectionStatus(tenantId);
        return Result.success(stats);
    }
    
    /**
     * 按分类统计第三方集成数量
     */
    @ApiOperation("按分类统计第三方集成数量")
    @GetMapping("/statistics/category")
    public Result<List<Map<String, Object>>> getStatsByCategory(HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<Map<String, Object>> stats = thirdPartyIntegrationService.getStatsByCategory(tenantId);
        return Result.success(stats);
    }
    
    /**
     * 按创建人统计第三方集成数量
     */
    @ApiOperation("按创建人统计第三方集成数量")
    @GetMapping("/statistics/created-by")
    public Result<List<Map<String, Object>>> getStatsByCreatedBy(HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<Map<String, Object>> stats = thirdPartyIntegrationService.getStatsByCreatedBy(tenantId);
        return Result.success(stats);
    }
    
    /**
     * 按时间统计第三方集成数量
     */
    @ApiOperation("按时间统计第三方集成数量")
    @GetMapping("/statistics/time")
    public Result<List<Map<String, Object>>> getStatsByTime(
            @ApiParam("开始时间") @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @ApiParam("结束时间") @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime,
            @ApiParam("时间单位") @RequestParam String timeUnit,
            HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<Map<String, Object>> stats = thirdPartyIntegrationService.getStatsByTime(startTime, endTime, timeUnit, tenantId);
        return Result.success(stats);
    }
    
    /**
     * 按月份统计第三方集成数量
     */
    @ApiOperation("按月份统计第三方集成数量")
    @GetMapping("/statistics/month")
    public Result<List<Map<String, Object>>> getStatsByMonth(@ApiParam("年份") @RequestParam Integer year,
                                                              HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<Map<String, Object>> stats = thirdPartyIntegrationService.getStatsByMonth(year, tenantId);
        return Result.success(stats);
    }
    
    /**
     * 获取连接成功率统计
     */
    @ApiOperation("获取连接成功率统计")
    @GetMapping("/statistics/connection-success-rate")
    public Result<Map<String, Object>> getConnectionSuccessRateStats(HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        Map<String, Object> stats = thirdPartyIntegrationService.getConnectionSuccessRateStats(tenantId);
        return Result.success(stats);
    }
    
    /**
     * 获取同步成功率统计
     */
    @ApiOperation("获取同步成功率统计")
    @GetMapping("/statistics/sync-success-rate")
    public Result<Map<String, Object>> getSyncSuccessRateStats(HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        Map<String, Object> stats = thirdPartyIntegrationService.getSyncSuccessRateStats(tenantId);
        return Result.success(stats);
    }
    
    /**
     * 获取平均响应时间统计
     */
    @ApiOperation("获取平均响应时间统计")
    @GetMapping("/statistics/avg-response-time")
    public Result<Map<String, Object>> getAvgResponseTimeStats(HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        Map<String, Object> stats = thirdPartyIntegrationService.getAvgResponseTimeStats(tenantId);
        return Result.success(stats);
    }
    
    /**
     * 获取性能统计
     */
    @ApiOperation("获取性能统计")
    @GetMapping("/statistics/performance")
    public Result<Map<String, Object>> getPerformanceStats(HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        Map<String, Object> stats = thirdPartyIntegrationService.getPerformanceStats(tenantId);
        return Result.success(stats);
    }
    
    /**
     * 获取错误统计
     */
    @ApiOperation("获取错误统计")
    @GetMapping("/statistics/error")
    public Result<Map<String, Object>> getErrorStats(HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        Map<String, Object> stats = thirdPartyIntegrationService.getErrorStats(tenantId);
        return Result.success(stats);
    }
    
    /**
     * 获取今日统计
     */
    @ApiOperation("获取今日统计")
    @GetMapping("/statistics/today")
    public Result<Map<String, Object>> getTodayStats(HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        Map<String, Object> stats = thirdPartyIntegrationService.getTodayStats(tenantId);
        return Result.success(stats);
    }
    
    /**
     * 获取本周统计
     */
    @ApiOperation("获取本周统计")
    @GetMapping("/statistics/weekly")
    public Result<Map<String, Object>> getWeeklyStats(HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        Map<String, Object> stats = thirdPartyIntegrationService.getWeeklyStats(tenantId);
        return Result.success(stats);
    }
    
    /**
     * 获取本月统计
     */
    @ApiOperation("获取本月统计")
    @GetMapping("/statistics/monthly")
    public Result<Map<String, Object>> getMonthlyStats(HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        Map<String, Object> stats = thirdPartyIntegrationService.getMonthlyStats(tenantId);
        return Result.success(stats);
    }
    
    /**
     * 查询分类列表
     */
    @ApiOperation("查询分类列表")
    @GetMapping("/categories")
    public Result<List<String>> getCategoryList(HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<String> list = thirdPartyIntegrationService.getCategoryList(tenantId);
        return Result.success(list);
    }
    
    /**
     * 查询集成类型列表
     */
    @ApiOperation("查询集成类型列表")
    @GetMapping("/integration-types")
    public Result<List<String>> getIntegrationTypeList(HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<String> list = thirdPartyIntegrationService.getIntegrationTypeList(tenantId);
        return Result.success(list);
    }
    
    /**
     * 查询系统类型列表
     */
    @ApiOperation("查询系统类型列表")
    @GetMapping("/system-types")
    public Result<List<String>> getSystemTypeList(HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<String> list = thirdPartyIntegrationService.getSystemTypeList(tenantId);
        return Result.success(list);
    }
    
    /**
     * 查询标签列表
     */
    @ApiOperation("查询标签列表")
    @GetMapping("/tags")
    public Result<List<String>> getTagList(HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<String> list = thirdPartyIntegrationService.getTagList(tenantId);
        return Result.success(list);
    }
    
    /**
     * 查询相关第三方集成
     */
    @ApiOperation("查询相关第三方集成")
    @GetMapping("/related/{integrationId}")
    public Result<List<ThirdPartyIntegration>> getRelatedIntegrations(@ApiParam("第三方集成ID") @PathVariable String integrationId,
                                                                      HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<ThirdPartyIntegration> list = thirdPartyIntegrationService.getRelatedIntegrations(integrationId, tenantId);
        return Result.success(list);
    }
    
    /**
     * 查询依赖关系
     */
    @ApiOperation("查询依赖关系")
    @GetMapping("/dependency/{integrationId}")
    public Result<List<Map<String, Object>>> getDependencyRelations(@ApiParam("第三方集成ID") @PathVariable String integrationId,
                                                                     HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<Map<String, Object>> list = thirdPartyIntegrationService.getDependencyRelations(integrationId, tenantId);
        return Result.success(list);
    }
    
    /**
     * 查询子第三方集成
     */
    @ApiOperation("查询子第三方集成")
    @GetMapping("/children/{parentId}")
    public Result<List<ThirdPartyIntegration>> getChildIntegrations(@ApiParam("父第三方集成ID") @PathVariable String parentId,
                                                                    HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<ThirdPartyIntegration> list = thirdPartyIntegrationService.getChildIntegrations(parentId, tenantId);
        return Result.success(list);
    }
    
    /**
     * 查询父第三方集成
     */
    @ApiOperation("查询父第三方集成")
    @GetMapping("/parent/{childId}")
    public Result<ThirdPartyIntegration> getParentIntegration(@ApiParam("子第三方集成ID") @PathVariable String childId,
                                                              HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        ThirdPartyIntegration integration = thirdPartyIntegrationService.getParentIntegration(childId, tenantId);
        return Result.success(integration);
    }
    
    /**
     * 查询第三方集成链
     */
    @ApiOperation("查询第三方集成链")
    @GetMapping("/chain/{integrationId}")
    public Result<List<ThirdPartyIntegration>> getIntegrationChain(@ApiParam("第三方集成ID") @PathVariable String integrationId,
                                                                   HttpServletRequest request) {
        String tenantId = (String) request.getAttribute("tenantId");
        List<ThirdPartyIntegration> list = thirdPartyIntegrationService.getIntegrationChain(integrationId, tenantId);
        return Result.success(list);
    }
    
    // 请求数据结构
    
    public static class BatchDeleteRequest {
        private List<String> ids;
        
        public List<String> getIds() { return ids; }
        public void setIds(List<String> ids) { this.ids = ids; }
    }
    
    public static class CopyIntegrationRequest {
        private String newName;
        
        public String getNewName() { return newName; }
        public void setNewName(String newName) { this.newName = newName; }
    }
    
    public static class BatchUpdateStatusRequest {
        private List<String> ids;
        private String status;
        
        public List<String> getIds() { return ids; }
        public void setIds(List<String> ids) { this.ids = ids; }
        public String getStatus() { return status; }
        public void setStatus(String status) { this.status = status; }
    }
    
    public static class BatchUpdatePriorityRequest {
        private List<String> ids;
        private Integer priority;
        
        public List<String> getIds() { return ids; }
        public void setIds(List<String> ids) { this.ids = ids; }
        public Integer getPriority() { return priority; }
        public void setPriority(Integer priority) { this.priority = priority; }
    }
    
    public static class BatchUpdateCategoryRequest {
        private List<String> ids;
        private String category;
        
        public List<String> getIds() { return ids; }
        public void setIds(List<String> ids) { this.ids = ids; }
        public String getCategory() { return category; }
        public void setCategory(String category) { this.category = category; }
    }
    
    public static class BatchUpdateSortOrderRequest {
        private List<String> ids;
        private Integer sortOrder;
        
        public List<String> getIds() { return ids; }
        public void setIds(List<String> ids) { this.ids = ids; }
        public Integer getSortOrder() { return sortOrder; }
        public void setSortOrder(Integer sortOrder) { this.sortOrder = sortOrder; }
    }
}